<template>
	<view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		dingSDK
	} from '@/common/js/dingding.js';
	import {
		mapState,
		mapGetters,
		mapMutations,
		mapActions
	}
	from 'vuex';
	import tabBar from "@/utils/tabBar.js"
	import {
		getUrlParameter
	} from '@/common/js/urils.js';
	import {
		UNITID,
		AR_UNITID,
		CC_UNITID
	} from "@/common/js/warehouse.js"
	import {
		getSysUserByPhones,
		getRelUserByMobile
	} from "@/common/api/ding/di_user.js"
	export default {
		data() {
			return {

			}
		},
		computed: {
			...mapGetters(['tabBarList', 'APP_USER', 'APP_DEPT', 'APPROVE_USER', 'APPROVE_DEPT', 'JURISDIC_LIST'])
		},
		async onLoad() {
			uni.showLoading({
				title: "加载中"
			})
			const auth_code = await this.getAuthCode()
			console.log("auth_code", auth_code)
			if (!auth_code) {
				this.$nextTick(() => {
					this.$refs.uToast.show({
						message: '未获取到赣政通授权key',
						type: 'error'
					});
				})
				return
			}
			// 查询用户信息
			this.queryUserInfo(auth_code)
			uni.hideLoading()
		},
		methods: {
			...mapMutations(['user/SET_DING_USER', 'user/SET_APP_DEPT', 'user/SET_APP_USER', 'user/SET_APPROVE_USER',
				'user/SET_APPROVE_DEPT', 'cascade/SET_JURISDIC'
			]),
			...mapActions(['getGztUserInfo', 'getSysUserInfo', 'getJurisdic']),
			// 跳转到行政复议
			goApprove() {
				if (!this.APPROVE_USER.id) {
					this.$refs.uToast.show({
						message: '未获取到审批系统用户信息',
						type: 'error'
					})
					return
				}
				if (!this.APPROVE_DEPT.parentid) {
					this.$refs.uToast.show({
						message: '未获取到审批系统部门信息',
						type: 'error'
					})
					return
				}
				sessionStorage.setItem('tabBarList', JSON.stringify(tabBar['user_approve']))
				uni.navigateTo({
					url: "/pages/approve/home/home"
				})
			},
			// 跳转到社区矫正
			async goCommunity() {
				if (!this.APPROVE_USER.id) {
					this.$refs.uToast.show({
						message: '未获取到审批系统用户信息',
						type: 'error'
					})
					return
				}
				if (!this.APPROVE_DEPT.parentid) {
					this.$refs.uToast.show({
						message: '未获取到审批系统部门信息',
						type: 'error'
					})
					return
				}
				console.log('this.APPROVE_DEPT', this.APPROVE_DEPT)
				const {
					data
				} = await this.getJurisdic(this.APPROVE_DEPT.parentid)
				data && this['cascade/SET_JURISDIC'](data);
				// console.log('JURISDIC_LIST', this.JURISDIC_LIST)
				// 根据身份判断跳转菜单
				if (this.APPROVE_DEPT.id == '3601001004') {
					// 市/区领导
					sessionStorage.setItem('tabBarList', JSON.stringify(tabBar['user_correction_leader']))
					uni.navigateTo({
						url: "/pages/correction/CorrectionHome"
					})
				} else {
					// 区县 / 司法所办事员
					sessionStorage.setItem('tabBarList', JSON.stringify(tabBar['user_correction_clerks']))
					uni.navigateTo({
						url: "/pages/correction/CorrectionHome"
					})
				}
			},
			// 跳转到错误处理页面
			goErrpage() {
				let description = '您所属科室的业务系统待开发'
				uni.navigateTo({
					url: "/pages/errPage?description=" + description
				})
			},
			// 跳转到被申请人答复
			async goRespond() {
				const rel_user = await getRelUserByMobile({
					mobile: this.APP_USER.mobile
				})
				if (rel_user.data) {
					sessionStorage.setItem('tabBarList', JSON.stringify(tabBar['user_correct']))
					uni.navigateTo({
						url: "/pages/respond/pend/pendList"
					})
					return
				}
			},
			// 查询授权code
			async getAuthCode() {
				// 测试
				let url = 'http://10.19.196.66:9800/sfdingtest-web/?auth_code=e893100dfd7d4d49be9ddd1d12a1ac0001729901'
				return await getUrlParameter(url, 'auth_code')
				// 正式
				// try {
				// 	let auth_code = getUrlParameter(window.location.href, 'auth_code');
				// 	if (!auth_code) {
				// 		auth_code = await this.empower();
				// 	}
				// 	return auth_code;
				// } catch (error) {
				// 	console.error('获取授权code失败', error);
				// 	throw error;
				// }
			},
			// 查询用户信息
			async queryUserInfo(auth_code) {
				// 1. 查询赣政通用户信息(测试需注释)
				// const gzt_data = await this.getGztUserInfo(auth_code)
				// console.log("gzt_user", gzt_data)
				// if (!gzt_data.data) {
				// 	this.$refs.uToast.show({
				// 		message: '未获取到赣政通用户信息',
				// 		type: 'error'
				// 	})
				// 	return
				// }
				// this['user/SET_DING_USER'](gzt_data.data);
				// 2. 查询系统用户信息
				// 正式
				// let userid = gzt_data.data.employeeCode.substring(4)
				// 测试
				let userid = "2201041336246CMjFsglhVSwPrYy0cp" // 龚欢
				// let userid = "201123184225Hqt0HYEm5ngSPyWqIYN" // 被申请人-黄旭
				// let userid = "201123184157Pr0d0WylKvG7334fWcc" // 万长俊
				// let userid = "220907110531xzh1mOYzlhOouoJoqoX" // 黄礼平
				const {
					data
				} = await this.getSysUserInfo([userid, {}])
				console.log("todo_user", data)
				if (!data) {
					this.$refs.uToast.show({
						message: '未获取到系统用户信息！',
						type: 'error'
					})
					return
				}
				const {
					approveUser,
					approveDepar,
					deptList,
					...sys_user
				} = data
				if (!(deptList && deptList.length)) {
					this.$refs.uToast.show({
						message: '未获取到用户部门信息！',
						type: 'error'
					})
					return
				}
				console.log("data", data)
				this['user/SET_APP_DEPT'](deptList[0]);
				this['user/SET_APP_USER'](sys_user);
				// 审批端人员信息
				this['user/SET_APPROVE_USER'](approveUser);
				this['user/SET_APPROVE_DEPT'](approveDepar ? approveDepar[0] : approveDepar);
				console.log('组织机构id', data.unitId)
				this.goRespond()
				if (data.unitId && data.unitId != UNITID) {
					let description = '暂无您需要处理的业务。'
					uni.navigateTo({
						url: "/pages/errPage?description=" + description
					})
					return
				} else {
					console.log('审批端人员信息', this.APPROVE_USER, this.APPROVE_DEPT)
					if (this.APPROVE_DEPT?.sectionid == AR_UNITID) {
						// this.goErrpage()
						this.goApprove()
					} else if (this.APPROVE_DEPT?.sectionid == CC_UNITID) {
						// this.goErrpage()
						this.goCommunity()
					} else {
						this.goErrpage()
					}
				}
			},
			// 获取免登授权码
			empower() {
				return new Promise((resolve, reject) => {
					// 加载钉钉sdk 获取免登授权码
					dingSDK().then((res) => {
						dd.getAuthCode({}).then((result) => {
							if (result) {
								resolve(result.code || result.auth_code)
							} else {
								reject("未获取到授权码")
							}
						}).catch((error) => {
							reject(error)
						});
					}).catch((error) => {
						reject(error)
					})
				})
			}
		}
	}
</script>

<style>
</style>